/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import ejb.Calendarejb;
import ejb.Eventejb;
import entity.Event;
import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.ConfigurableNavigationHandler;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.ScheduleEvent;

/**
 *
 * @author user
 */
@Named
@RequestScoped

public class Eventbean{

    @Inject
    private Eventejb eventejb;
    
    @Inject
    private Calendarejb calejb;
    
    @Inject
    private Calendarbean calendarbean;

    private Event event;
    
    private String namechange, eventtodelete;
    private Date dateeventtodelete, dateeventtochange;
    
    public Date getDateeventtochange(){
        return dateeventtochange;
    }
    
    public void setDateeventtochange(Date dateeventtochange){
        this.dateeventtochange = dateeventtochange;
    }
    
    public Date getDateeventtodelete() {
        return dateeventtodelete;
    }

    public void setDateeventtodelete(Date dateeventtodelete) {
        this.dateeventtodelete = dateeventtodelete;
    }
    
    public String getEventtodelete() {
        return this.eventtodelete;
    }

    public void setEventtodelete(String name) {
        this.eventtodelete = name;
    }
    
    public String getNamechange(){
        return this.namechange;
    }
    
    public void setNamechange (String name){
        this.namechange = name;
    }

    public Event getEvent() {
        if (event == null) {
            event = new Event();
        }
        return event;
    }

    public void setEvent(Event event) {
        this.event = event;
    }
    
    public String createEvent (){
  
        eventejb.createEvent(event);
        return "/s/user/calendar?faces-redirect=true";
    }
    
    public String updateEvent(){
        eventejb.updateEvent(event, this.namechange, dateeventtochange);
        return "/s/user/calendar?faces-redirect=true";
        
    }
    
    public String deleteEvent(){
        eventejb.deleteEvent(eventtodelete,dateeventtodelete);
        return "/s/user/calendar?faces-redirect=true";
    }
    
    public void onEventSelect(SelectEvent selectEvent) {

        ScheduleEvent scheduleevent = (ScheduleEvent) selectEvent.getObject();
        event = calejb.findEventbyName(scheduleevent.getTitle(), scheduleevent.getStartDate());
        System.out.println(scheduleevent.getTitle());
        System.out.println(event.getName());

        ConfigurableNavigationHandler configurableNavigationHandler = (ConfigurableNavigationHandler) FacesContext.getCurrentInstance().getApplication().getNavigationHandler();
        configurableNavigationHandler.performNavigation("/s/user/showevent?faces-redirect=true");
//        ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
//        try {
//            context.redirect(context.getRequestContextPath() + "/s/user/showevent.xhtml?faces-redirect=true");
//
//        } catch (IOException ex) {
//
//        }
    }
}
